package com.market.interceptor;

import com.market.enums.RoleEnum;
import com.market.utils.TokenUtil;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @ClassName ManagerInterceptor
 * @Description TODO
 * @Author 1
 * @Date 2022/4/3 23:02
 * @Version 1.0
 */
@Component
public class ManagerInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        boolean flag=false;
        String token=request.getHeader("Token");
        if(token==null){
            System.out.println("您还未登录,请先登录！");
            return false;
        }
        Map<String, Object> map = TokenUtil.parseToken(token);
        if((map.get("role").equals(RoleEnum.MANAGER.getRoleName()))||(map.get("role").equals(RoleEnum.SUPER_MANAGER.getRoleName()))){
            flag=true;
        }else{
            System.out.println(map.get("role"));
            System.out.println(RoleEnum.MANAGER.getRoleName());
            System.out.println("不是管理员");
        }
        return flag;
    }
}
